package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.AlbumHeaderShowBean;
import com.elinkway.infinitemovies.bean.AlbumShowBean;
import com.elinkway.infinitemovies.bean.HomePageList;
import com.elinkway.infinitemovies.bean.IntervalBean;
import com.elinkway.infinitemovies.bean.RecommendBase;
import com.elinkway.infinitemovies.dao.ChannelContentDao;
import com.elinkway.infinitemovies.utils.MoviesConstant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class HomePageParser extends LVideoMobileParser<HomePageList> {
	
	private final String FOCUS = "focus";
	
	private final String REC = "rec";

	private String page; // 频道page

	private boolean isNeedCache = true; // 是否需要缓存

	public HomePageParser (String page,boolean isNeedCache) {
		this.page = page;
		this.isNeedCache = isNeedCache;
	}
	
	@Override
	public HomePageList parse(JSONObject data) throws Exception {
		if (null != data) {
			try {
//				JSONObject dataObj = data.optJSONObject("data");
//				if (null == dataObj) {
//					return null;
//				}
				HomePageList mainPageData = new HomePageList();
				ArrayList itemList = new ArrayList();

				JSONArray galleryArray = data.optJSONArray(FOCUS);
				if (null != galleryArray && galleryArray.length() > 0) {
					int galleryLength = galleryArray.length();
					ArrayList galleryList = new ArrayList();
					for (int i = 0; i < galleryLength; i++) {
						RecommendBase recBase = new RecommendBase();
						JSONObject obj = galleryArray.optJSONObject(i);
						getCommonRecommenData(obj, recBase);
						galleryList.add(recBase);
					}
					itemList.add(galleryList);
				}

				JSONArray recommendDataArray = data.optJSONArray(REC);
				if (null != recommendDataArray && recommendDataArray.length() > 0) {
					int recArrayLength = recommendDataArray.length();
					for (int i = 0; i < recArrayLength; i++) {
						JSONObject recommendObj = recommendDataArray
								.optJSONObject(i);
						AlbumHeaderShowBean headerShowBean = new AlbumHeaderShowBean();
						headerShowBean.setRecid(recommendObj.optString("recid"));
						headerShowBean.setRecname(recommendObj.optString("recname"));
						headerShowBean.setVt(recommendObj.optString("vt"));
						headerShowBean.setCategoryid(recommendObj.optString("categoryid"));
						headerShowBean.setAreaid(recommendObj.optString("areaid"));
						headerShowBean.setStyle(recommendObj.optString("style"));
						headerShowBean.setUnLimit(recommendObj.optString("unlimit"));
						headerShowBean.setIsPull(recommendObj.optString("is_pull"));

						if(MoviesConstant.STYLE_FEED.equals(headerShowBean.getStyle()) && "1".equals(headerShowBean.getUnLimit())){
							mainPageData.setUnlimitFeedId(headerShowBean.getRecid());
							mainPageData.setUnlimitFeedVt(headerShowBean.getVt());
						}
						if(MoviesConstant.STYLE_FEED.equals(headerShowBean.getStyle()) && "1".equals(headerShowBean.getIsPull())){
							mainPageData.setPullToMoreFeedId(headerShowBean.getRecid());
							mainPageData.setPullToMoreFeedVt(headerShowBean.getVt());
						}

						itemList.add(headerShowBean);

						JSONArray recJSONArray = recommendObj.optJSONArray("data");
						if (null != recJSONArray && recJSONArray.length() > 0) {
							int length = recJSONArray.length();
							for (int j = 0; j < length; j++) {
								JSONObject recItemObj = recJSONArray.optJSONObject(j);
								AlbumShowBean albumShowBean = new AlbumShowBean();
								albumShowBean.setRecid(headerShowBean.getRecid());
								albumShowBean.setRecname(headerShowBean.getRecname());
								albumShowBean.setCurrentShowIndex(j);
								getCommonAlbumData(recItemObj, albumShowBean);
								if(MoviesConstant.STYLE_SPECIAL.equals(headerShowBean.getStyle())){
									albumShowBean.setType(MoviesConstant.MODEL_SPECIAL);
									itemList.add(albumShowBean);
								}else if(MoviesConstant.STYLE_TWO.equals(headerShowBean.getStyle())){
									albumShowBean.setType(MoviesConstant.MODEL_HALF);
									itemList.add(albumShowBean);
									if(j%2 == 0){
										itemList.add(new IntervalBean());
									}
								}else if(MoviesConstant.STYLE_THREE.equals(headerShowBean.getStyle())){
									albumShowBean.setType(MoviesConstant.MODEL_THIRD);
									itemList.add(albumShowBean);
									if(j%3 == 0 || j%3==1){
										itemList.add(new IntervalBean());
									}
								}else if(MoviesConstant.STYLE_FEED.equals(headerShowBean.getStyle())){
									albumShowBean.setType(MoviesConstant.MODEL_FEED);
									itemList.add(albumShowBean);
								}else if(MoviesConstant.STYLE_MIXTURE_TWO.equals(headerShowBean.getStyle())){
									if(j == 0){
										albumShowBean.setType(MoviesConstant.MODEL_FULL);
										itemList.add(albumShowBean);
									}else{
										albumShowBean.setType(MoviesConstant.MODEL_HALF);
										itemList.add(albumShowBean);
										if((j-1)%2 == 0){
											itemList.add(new IntervalBean());
										}
									}
								}else if(MoviesConstant.STYLE_MIXTURE_THREE.equals(headerShowBean.getStyle())){
									if(j == 0){
										albumShowBean.setType(MoviesConstant.MODEL_FULL);
										itemList.add(albumShowBean);
									}else{
										albumShowBean.setType(MoviesConstant.MODEL_THIRD);
										itemList.add(albumShowBean);
										if((j-1)%3 == 0 || (j-1)%3==1){
											itemList.add(new IntervalBean());
										}
									}
								}else{
									albumShowBean.setType(MoviesConstant.MODEL_THIRD);
									itemList.add(albumShowBean);
								}
							}
						}
						if(i == 0){//增加广告位
							mainPageData.setHomeBannerAdIndex(itemList.size());
						}
					}
				}

				mainPageData.setPageItemList(itemList);
				if (isNeedCache) {
					ChannelContentDao dao = new ChannelContentDao(MoviesApplication.getInstance());
					dao.saveJson(data.toString(),page);
				}
				return mainPageData;
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 焦点图解析
	 * @param obj
	 * @param recBase
     */
	private void getCommonRecommenData(JSONObject obj, RecommendBase recBase) {
		recBase.setName(obj.optString("name"));
		recBase.setAid(obj.optString("aid"));
		recBase.setThemeid(obj.optString("themeid"));
		recBase.setPlayurl(obj.optString("playurl"));
		recBase.setDisplay(obj.optString("display"));
		recBase.setSubname(obj.optString("subname"));
		recBase.setPic(obj.optString("pic"));
		recBase.setVt(obj.optString("vt"));
		recBase.setShowtime(obj.optString("showtime"));
		recBase.setCornerColor(obj.optString("cornerColor"));
		recBase.setCornerTitle(obj.optString("cornerTitle"));
	}
	
	/**
	 * 获取通用的解析数据
	 * @param obj
	 */
    private void getCommonAlbumData(JSONObject obj, AlbumShowBean albumBean) {
		albumBean.setName(obj.optString("name"));
		albumBean.setAid(obj.optString("aid"));
		albumBean.setThemeid(obj.optString("themeid"));
		albumBean.setPlayurl(obj.optString("playurl"));
		albumBean.setDisplay(obj.optString("display"));
		albumBean.setSubname(obj.optString("subname"));
		albumBean.setPic(obj.optString("pic"));
		albumBean.setPich(obj.optString("pich"));
		albumBean.setVt(obj.optString("vt"));
		albumBean.setShowtime(obj.optString("showtime"));
		albumBean.setNowepisodes(obj.optString("nowepisodes"));
		albumBean.setPicsize(obj.optString("picsize"));
		albumBean.setEpisodes(obj.optString("episodes"));
		albumBean.setIsend(obj.optString("isend"));
		albumBean.setRating(obj.optString("rating"));
		albumBean.setCategoryname(obj.optString("category_name"));
		albumBean.setDescription(obj.optString("description"));
		albumBean.setPlaycount(obj.optString("play_count"));
		albumBean.setInfo(obj.optString("info"));

		albumBean.setCornerColor(obj.optString("cornerColor"));
		albumBean.setCornerTitle(obj.optString("cornerTitle"));
		albumBean.setEposidesInfo(albumBean.getEpisodeInfoInString());
    }
	
}
